package com.ccepc.algmanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccepc.algmanage.service.PushRuleService;
import com.ccepc.algmanage.utils.AuthInfoUtils;
import com.ccepc.algmanage.utils.CommonUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.demo.api.entity.DelBatch;
import com.ccepc.demo.api.entity.PushRule;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-17 17:38:29
 */
@RestController
@RequestMapping("/pushrule" )
@Api(value = "pushrule", tags = "推送规则")
public class PushRuleController {

    @Resource
    private PushRuleService pushRuleService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('algmanage_pushrule_view')" )
    public R getPushRulePage(Page<PushRule> page, PushRule pushRule) {
        return R.ok(pushRuleService.page(
                page, new LambdaQueryWrapper<PushRule>()
                        .like(pushRule.getRuleName() != null, PushRule::getRuleName, pushRule.getRuleName())
                        .eq(PushRule::getTenantId, AuthInfoUtils.getTenantId())));
    }

    @ApiOperation(value = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('algmanage_pushrule_view')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(pushRuleService.getById(id));
    }

    @ApiOperation(value = "新增或修改")
    @SysLog("新增或修改" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('algmanage_pushrule_add')" )
    public R save(@RequestBody PushRule pushRule) {
        return pushRuleService.saveOrUpdateP(pushRule);
    }

    @ApiOperation(value = "通过ids删除")
    @SysLog("通过ids删除" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('algmanage_pushrule_del')" )
    public R removeById(@RequestBody DelBatch delBatch) {
        return R.ok(pushRuleService.removeByIds(delBatch.getIds()));
    }

}
